﻿using System;

namespace Kernelx.Common.Threading
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class ConcurrentContainer<TData> where TData : class
    {
        private readonly string _key;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key">未指定时,内部构建一个随机key</param>
        public ConcurrentContainer(string key = null) => _key = !string.IsNullOrEmpty(key) ? key : Guid.NewGuid().ToString();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public void Set(TData data) => ConcurrentContainer.SetData(_key, data);

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsExist() => ConcurrentContainer.GetData(_key) != null;


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public TData Get() => ConcurrentContainer.GetData(_key) as TData;

    }
}
